#!/bin/bash
# kobo-restore version 0.1-$Rev$
# Restore a corrupted Kobo database
# 
# This script will back up your Kobo's database to a local directory
# and dump it to an SQL file using the SQLite3 tools, Then it will 
# use that dump to create a new, fixed database and copy it back to 
# your Kobo.
#
# Note that this script has not been extensively tested, but I 
# believe it to be pretty solid. If you have any additions, 
# corrections, improvements or complaints please feel free to get in
# touch with the author.
#
# This software is free, simultaneously released under the MIT open-
# source license and the cc-by-3.0 Creative Commons license. This 
# means you are free to use and disseminate it however you like, so 
# long as you attribute the original authors for our work. See the
# individual licenses, included with this file, for details.
#
# The MIT License (MIT)
# Copyright (c) 2011 David Leaman
#
# Permission is hereby granted, free of charge, to any person 
# obtaining a copy of this software and associated documentation 
# files (the "Software"), to deal in the Software without 
# restriction, including without limitation the rights to use, copy, 
# modify, merge, publish, distribute, sublicense, and/or sell copies 
# of the Software, and to permit persons to whom the Software is 
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be 
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
# DEALINGS IN THE SOFTWARE.

# Many thanks to Tim Davidson, who provided the core functionality
# of the Kobo database restoration:
# http://community.indigo.ca/posts/Devon-Veldhuis/user-442705/586884.html

### CONFIGURATION ###

# Set this to the path to your Kobo's root directory:
KOBO_MOUNT_POINT=/media/KOBOeReader

# Set this to your SQLite command:
SQLITE=sqlite3

### END CONFIGURATION ###
### You shouldn't need to modify anything below this line ###

# Check that we have everything we need
declare -a errors ; errnum=0
if [[ -z "`which $SQLITE`" ]]
then 
	errors[$((errnum++))]="SQLite command '$SQLITE' not found."
	errors[$((errnum++))]="	Check that SQLite is installed or change the SQLITE variable."
fi
if [[ ! -d $KOBO_MOUNT_POINT ]]
then 
	errors[$((errnum++))]="Invalid Kobo path: $KOBO_MOUNT_POINT"
	errors[$((errnum++))]="	Make sure your Kobo is connected and that KOBO_MOUNT_PATH is set correctly."
elif [[ ! -r $KOBO_MOUNT_POINT ]]
then
	errors[$((errnum++))]="Could not read Kobo path: $KOBO_MOUNT_POINT"
	errors[$((errnum++))]="	Make sure you have permission to read this path."
fi
if [[ $errnum -ne 0 ]]
then
	echo Not performing Kobo restore because of errors:
	echo
	for err in "${errors[@]}" ; do IFS=' '; echo $err ; done
	echo exiting...
	exit 1
fi

SUFFIX=sqlite
DBNAME=KoboReader.$SUFFIX
BASENAME=`basename $DBNAME .$SUFFIX`
KOBODB=$KOBO_MOUNT_POINT/.kobo/$DBNAME
BAKDIR=~/.kobo/bak-`date +%Y%m%d.%H%M%S`
BAKDB=$BAKDIR/$DBNAME
DUMPFILE=$BAKDIR/$BASENAME.dump.txt
NEWDB=$BAKDIR/$BASENAME-fixed.$SUFFIX

# Create backup directory
mkdir -p $BAKDIR
echo Backing up Kobo database to $BAKDB...
cp $KOBODB $BAKDB || {
	echo "Could not copy $KOBODB to $BAKDB."
	echo "	Check your permissions."
	exit 1
}
echo Extracting database schema...
echo .dump | $SQLITE $BAKDB > $DUMPFILE || {
	echo "Could not dump Kobo database."
	echo "	You can try again, but if that doesn't work"
	echo "	I don't think I can help you. Sorry."
	exit 1
}
echo Recreating database...
echo .quit | $SQLITE -init $DUMPFILE $NEWDB || {
	echo "Could not re-initialize database."
	echo "	You can try again, but if that doesn't work"
	echo "	I don't think I can help you. Sorry."
	exit 1
}
echo Restoring database on Kobo device in 5 seconds. 
echo -n Press Ctrl-C to cancel.
while [[ $((++count)) -le 5 ]] ; do 
	sleep 1 ; 
	echo -n " "$((5 - $count)) ; 
done ; echo .
echo Restoring database on Kobo device...
cp $NEWDB $KOBODB || {
	echo "Could not restore Kobo database."
	echo "	Check your permissions, and make sure your"
	echo "	Kobo is properly mounted, then try again."
	exit 1
}
echo Success!
echo Now unmount your Kobo and enjoy reading.
echo

